
import string, random

def randString(size=6, chars=string.ascii_uppercase + string.digits):
    return "".join(random.choice(chars) for x in range(size))

class Task:
    def __init__(self, t, des=None, dat=None,
                 own=None, prior=None, iden=None, val=[False, False],
                 java="toggleCheck(this)", child=None):
        self.title = t
        self.descrip = des
        self.date = dat
        self.owner = own
        self.priority = prior
        if child:
            self.children = child
        else:
            self.children = []
        self.value = val
        self.javascript = java
        if iden:
            self.identity = iden
        else:
            self.identity = randString()
    
    def toHTML(self):
        task = ""
        task +=  self.title
        if self.descrip:
            task += ": " + self.descrip
        if self.owner:
            task += "<br><i>" + self.owner + "</i>"
        if self.date:
            if self.owner:
                task += " - "
            else:
                task += "<br>Due: "
            task += "<b>" + str(self.date) + "</b>"
        if self.priority:
            task += "<br>Priority: " + str(self.priority) + "</br>"
        
        return task

    def buildHTML(self):
        return (self.toTableEntry() + "\n<tr><td>&nbsp</td><td>" +
                self.childHTML() + "</td></tr>\n" +
                "<tr><td>&nbsp</td><td>" + self.createAdder() +
                "</td></td>")
        
    def rootHTML(self):
        return ("<table>\n" + self.buildHTML() +
#                "<tr><td>" + self.createAdder() + "</td></tr>" + 
                "\n</table>")

    def childHTML(self):
        if len(self.children) == 0:
            return ""
        txt = "<table>\n"
        for t in self.children:
            txt += t.buildHTML()
        return txt + "\n</table>"

    def makeCheckbox(self, sec=False):
        if not sec and self.value[0] or sec and self.value[1]:
            chk = "checked"
        else:
            chk = ""
        if sec:
            n = "-sec"
        else:
            n = ""
        return ("<input type=\"checkbox\" " +
                "id=\"" + self.identity + n + "\" " +
                "onchange=\"" + self.javascript + "\" " +
                chk +
                "/>")

    def toTableEntry(self):
        return  ("<tr>" +
                 "<td>" + self.makeCheckbox() +
                 self.makeCheckbox(True) +
                 "<td>" + self.toHTML() + "</td>" +
                 "</tr>")

    def addChild(self, c):
        self.children.append(c)

    def toArray(self):
        return [self.title, self.descrip, self.date,
                self.owner, self.priority, self.identity,
                self.value, self.javascript,
                [c.toArray() for c in self.children]]

    def fromArray(self, ar):
        return Task(ar[0], ar[1], ar[2], ar[3],
                    ar[4], ar[5], ar[6], ar[7],
                    [self.fromArray(c) for c in ar[8]])

    def createAdder(self):
        txt = ("<div>" +
               "<button id=\"" + self.identity +"-submit\"" +
               "onclick=addTask(this)>" +
               "Add</button>" +
               "<input id=\"" + self.identity + "-name\" " +
               "placeholder=\"Task Name\" \>" +
               "</div>")
        return txt


    def changeTask(self, diff, sec=0):
        t = self.findTask(diff)
        if t:
            t.chMark(i_val=sec)
            return True
        
    def chMark(self, m=None, i_val=0):
        if m == None:
            self.value[i_val] = not self.value[i_val]
        else:
            self.value[i_val] = m
        for c in self.children:
            c.chMark(self.value, i_val)

    def findTask(self, num):
        if self.identity == num:
            return self
        for c in self.children:
            t = c.findTask(num)
            if t:
                return t
        return None

    def deleteChecked(self):
        i=0
        while i < len(self.children):
            if self.children[i].value[0]:
                self.children.pop(i)
            else:
                self.children[i].deleteChecked()
                i += 1

def loadTasks(diff=None):
    f = open("tasks.dat", "a+")
    txt = f.read()
    if len(txt.strip()) == 0:
        f.write(str(Task("Main").toArray()))
        f.close()
        f = open("tasks.dat", "a+")
        txt = f.read()
    t = Task("Dummy")
    t = t.fromArray(eval(txt))
    f.close()
    if diff:
        i=diff.find("-sec")
        if diff.find("-sec") >= 0:
            diff = diff[:i]
    if diff and t.changeTask(diff, 1*(i>=0)):
        f = open("tasks.dat", "w")
        f.write(str(t.toArray()))
        f.close()
    return t.rootHTML()
